package com.sun.book.activity;

import java.util.ArrayList;
import java.util.List;

import com.sun.book.R;
import com.sun.book.adapter.HomepageBtnAdapter;
import com.sun.book.db.IconsDb;
import com.sun.book.itemdata.ShakeButton;
import com.sun.book.port.Delete;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.GridView;
import android.widget.LinearLayout;

public class HomePageActivity  extends LinearLayout  implements Delete{
	GridView mHomepageGridView;
	HomepageBtnAdapter mBtnAdapter;
	private Context mContext;
	int[] mHpIcon ;
	
	int[] mHpPressIcon;
	String[] mHpIconString;
	String[] mHpPressIconString;
	ArrayList<ShakeButton> mShakeButtons = new ArrayList<ShakeButton>();
	private IconsDb iconsDb;
	private int[] HpIcon;
	private Handler handler;
	
	public HomePageActivity(Context context) {
		super(context);
	 
		mContext = context;
		
		this.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
		LayoutInflater factory = LayoutInflater.from(context);
		factory.inflate(R.layout.homepage_layout, this);

		mHomepageGridView = (GridView) findViewById(R.id.homepage);
		initShakeButtons();
		createGrid();
		}
	
	private void createGrid() {
		iconsDb=new IconsDb();
		iconsDb.inintDB(getContext());
		mHpIconString=iconsDb.getmHpPressIcon();
		mHpPressIconString=iconsDb.getmHpPressIcon();
		mHpIcon=zhuanhua(mHpIconString);
		mHpPressIcon=zhuanhua(mHpPressIconString);
		iconsDb.close();
		mBtnAdapter = new HomepageBtnAdapter(mContext, mShakeButtons);
		mBtnAdapter.setHpDefaultIcon(mHpIcon);
		mBtnAdapter.setHpPressIcon(mHpPressIcon);
		mHomepageGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mHomepageGridView.setAdapter(mBtnAdapter);	 
		
	}

	public void initShakeButtons(){
		iconsDb=new IconsDb();
		iconsDb.inintDB(mContext);
		mHpIconString=iconsDb.getmHpPressIcon();
		mHpPressIconString=iconsDb.getmHpPressIcon();
		mHpIcon=zhuanhua(mHpIconString);
		for(int j=0;j<mHpIconString.length;j++){
			System.out.println(mHpIconString[j]);
		}
		mHpPressIcon=zhuanhua(mHpPressIconString);
		iconsDb.close();
		int count = mHpIcon.length;
		
		for(int i = 0; i< count; i++){
			ShakeButton button = new ShakeButton(mContext,handler);
			button.setEventListener(this);
			button.setContainer(mHomepageGridView);
			button.setLayoutParams(new GridView.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT));
			
			mShakeButtons.add(button);
		}
	}
	 
	private int[] zhuanhua(String[] mHpIconString2) {
		HpIcon=new int[mHpIconString2.length];
	 for(int i=0;i<mHpIconString2.length;i++){
		 HpIcon[i]=R.drawable.yisou;
		 if(mHpIconString2[i].equals("yisou")){
			 HpIcon[i]=R.drawable.yisou;	 
		  }else 
		 if(mHpIconString2[i].equals("tadu")){
			  HpIcon[i]=R.drawable.tadu;
	  }
			 else if(mHpIconString2[i].equals("baidu")){
			  HpIcon[i]=R.drawable.baidu;
		  }else if(mHpIconString2[i].equals("hxtx")){
			  HpIcon[i]=R.drawable.hxtx;
		  }
		  else if(mHpIconString2[i].equals("jinjiang")){
			  HpIcon[i]=R.drawable.jinjiang;
		  }else if(mHpIconString2[i].equals("k")){
			  HpIcon[i]=R.drawable.k;
		  }else if(mHpIconString2[i].equals("qidian")){
			  HpIcon[i]=R.drawable.qidian;
		  }
		  else if(mHpIconString2[i].equals("qq")){
			  HpIcon[i]=R.drawable.qq;
		  }else if(mHpIconString2[i].equals("shupeng")){
			  HpIcon[i]=R.drawable.shupeng;
		  }else if(mHpIconString2[i].equals("shuqi")){
			  HpIcon[i]=R.drawable.shuqi;
		  }
		 else if(mHpIconString2[i].equals("xiaoxiang")){
			  HpIcon[i]=R.drawable.xiaoxiang;
		  }else if(mHpIconString2[i].equals("yanqing")){
			  HpIcon[i]=R.drawable.yanqing;
		  }else if(mHpIconString2[i].equals("yidong")){
			  HpIcon[i]=R.drawable.yidong;
		  }else if(mHpIconString2[i].equals("zongheng")){
			  HpIcon[i]=R.drawable.zongheng;
		  }
	 }
		return HpIcon;
	}

	public void stopShakeing(){
		int count = mHomepageGridView.getCount();
		
		if(mHomepageGridView != null && count != 0){			
			for (int i = 0; i < count; i++) {
				ShakeButton sb = (ShakeButton) (mHomepageGridView.getItemAtPosition(i));
				if (sb.mState == ShakeButton.ANIM) {					
					sb.stopShakeAnimation();
					sb.mState = ShakeButton.NORAML;
					sb.postInvalidate();
				}
			}
		}
	}

	public GridView getHomePageGridView() {
		if (mHomepageGridView != null)
			return mHomepageGridView;
		return null;
	}

	@Override
	public void delete(int i) {
		iconsDb=new IconsDb();
		iconsDb.inintDB(mContext);
		iconsDb.delete(i);
		
		mHpIconString=iconsDb.getmHpPressIcon();
		mHpPressIconString=iconsDb.getmHpPressIcon();
		iconsDb.close();
		mHpIcon=zhuanhua(mHpIconString);
		mHpPressIcon=zhuanhua(mHpPressIconString);
		
		mBtnAdapter.setHpDefaultIcon(mHpIcon);
		mBtnAdapter.setHpPressIcon(mHpPressIcon);
		mBtnAdapter.notifyDataSetChanged();
	}

	
	
	
	

}
